﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethodCore
{
    /// <summary>
    /// 排序类
    /// </summary>    
    public static class SortHelper
    {
        /// <summary>
        /// list集合排序
        /// </summary>
        /// <param name="shapingArray">需要排序的数组</param>
        /// <param name="PositiveSequence">是否升序，默认升序</param>
        /// <returns></returns>
        public static int[] Sort(this int[] shapingArray, bool PositiveSequence = true)
        {
            var list = shapingArray.ToList();
            if (PositiveSequence)
            {
                list.Sort();
            }
            else
            {
                list.Sort((x, y) => -x.CompareTo(y));
            }

            return list.ToArray();
        }

        /// <summary>
        /// 浮点数数组冒泡排序
        /// </summary>
        /// <param name="shapingArray">需要排序的浮点数数组</param>
        /// <param name="PositiveSequence">是否升序</param>
        /// <returns></returns>
        public static double[] BubbleSort(this double[] shapingArray, bool PositiveSequence = true)
        {
            double temp = 0;
            if (PositiveSequence)
            {
                for (int i = 0; i < shapingArray.Length - 1; i++)
                {
                    for (int j = i+1; j < shapingArray.Length; j++)
                    {
                        if (shapingArray[i]> shapingArray[j])
                        {
                            temp = shapingArray[i];
                            shapingArray[i] = shapingArray[j];
                            shapingArray[j] = temp;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < shapingArray.Length - 1; i++)
                {
                    for (int j = i + 1; j < shapingArray.Length; j++)
                    {
                        if (shapingArray[i] < shapingArray[j])
                        {
                            temp = shapingArray[i];
                            shapingArray[i] = shapingArray[j];
                            shapingArray[j] = temp;
                        }
                    }
                }
            }
           
            return shapingArray;
        }

    }
}
